package Leetcode250618.array;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 无重复字符的最长子串
 * @author: ChenYawei
 * @date: 2025/6/18  17:40
 */
// 滑动窗口
public class LengthOfLongestSubstring03 {

    public static void main(String[] args) {
        String s = "abba";
        LengthOfLongestSubstring03 ob = new LengthOfLongestSubstring03();
        int i = ob.lengthOfLongestSubstring(s);
        System.out.println(i);
    }

    public int lengthOfLongestSubstring(String s) {
        if (s.isEmpty()) {
            return 0;
        }
        int left = 0, right = 0;
        int max = Integer.MIN_VALUE;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                int index = map.get(c) + 1;
                left = Math.max(left, index);
            }
            map.put(c, i);
            right = i;
            max = Math.max(max, (right - left + 1));
        }
        return max;
    }

}
